package com.zhuss.controller.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zhuss.bean.bo.core.auth.AuthBo;
import com.zhuss.bean.bo.core.login.UpdateBo;
import com.zhuss.bean.bo.core.login.UpdatePicBo;
import com.zhuss.bean.bo.core.login.UpdatePwdBo;
import com.zhuss.bean.bo.core.login.wechat.BindBo;
import com.zhuss.bean.bo.core.login.wechat.GetOpenIdBo;
import com.zhuss.bean.bo.core.login.wechat.GetUsernameBo;
import com.zhuss.bean.bo.core.login.wechat.InitBo;
import com.zhuss.bean.bo.core.login.wechat.LoginChatBo;
import com.zhuss.bean.bo.core.login.wechat.SendCodeBo;
import com.zhuss.bean.bo.core.login.wechat.UnbindBo;
import com.zhuss.bean.vo.core.login.GetUsernameVo;
import com.zhuss.common.BaseController;
import com.zhuss.common.LoginOperateService;
import com.zhuss.core.login.LoginServiceWechat;
import com.zhuss.exception.WpsException;
import com.zhuss.third.WXThirdService;
import com.zhuss.util.AES;
import com.zhuss.util.ForceSmallUtil;


/**
 * wechat
 * 登录操作 controller
 * @Author zhushanshuo
   @Date 2020-03-25 19:58
 *
 */
  
 
@RestController
@RequestMapping("/login")
public class LoginController extends BaseController {
	
	@Autowired
	@Qualifier("loginService")
	private LoginOperateService<Object, Object> loginService;
	
	@Autowired
	@Qualifier("loginServiceWechat")
	private LoginServiceWechat loginServiceWechat;
	
	@Autowired
	private WXThirdService wxThirdService;
	
	
	/**
	 * 手机号 
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/getUsername")
    public Object getUsername(@RequestBody GetUsernameBo bo) throws WpsException {
		
		String encrypted = bo.getEncrypted();
		String sessionKey = bo.getSessionKey();
		String iv = bo.getIv();
		String username = AES.decodeUserInfo(encrypted, sessionKey, iv);
		GetUsernameVo result = new GetUsernameVo();
		result.setUsername(username);
		return result;
    }
	
	
	
	/**
	 * 初始化 
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/getOpenId")
    public Object getOpenId(@RequestBody GetOpenIdBo bo) throws WpsException {
		
		Object result = wxThirdService.query(bo.getJscode(), bo.getAppId(), bo.getAppSecret());
		return result;
    }
	
	/**
	 * 初始化 
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/menu")
    public Object menu(@RequestBody AuthBo bo) throws WpsException {
		
		Object result = loginServiceWechat.menu(bo);
		return result;
    }
	
	/**
	 * 初始化 
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/init")
    public Object init(@RequestBody InitBo bo) throws WpsException {
		
		Object result = loginServiceWechat.init(bo);
		return result;
    }
	
	
	/**
	 * 绑定
	 * 登录
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/bind")
    public Object bind(@RequestBody BindBo bo) throws WpsException {
		
		Object result = loginServiceWechat.bind(bo);
		return result;
    }
	
	/**
	 * 注册 or
	 * 登录
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
	@PostMapping("/login")
	public Object login(@RequestBody LoginChatBo bo) throws WpsException {
		
		Object result = loginServiceWechat.loginChat(bo);
		return result;
	}
	
	
	/**
	 * 验证码
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
	@PostMapping("/sendCode")
	public Object sendCode(@RequestBody SendCodeBo bo) throws WpsException {
		
		Object result = loginServiceWechat.sendCode(bo);
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					ForceSmallUtil.forceSmallHistory();
				} catch (WpsException e) {
					e.printStackTrace();
				}
			}
		}).start(); 
		return result;
	}
	
	
	/**
	 * 解绑
	 * 登出
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/unbind")
    public void unbind(@RequestBody UnbindBo bo) throws WpsException {
		
		loginServiceWechat.unbind(bo);
    }
	
	/**
	 * 修改密码
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/pwdUpdate")
    public void pwdUpdate(@RequestBody UpdatePwdBo bo) throws WpsException {
		
		loginServiceWechat.updatePwd(bo);
    }
	
	/**
	 * wechat
	 * 修改头像
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/picUpdate")
    public void picUpdate(@RequestBody UpdatePicBo bo) throws WpsException {
		
		loginServiceWechat.updatePic(bo);
    }
	
	/**
	 * wechat
	 * 修改
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
	@PostMapping("/update")
	public void update(@RequestBody UpdateBo bo) throws WpsException {
		
		loginServiceWechat.update(bo);
	}

	
}
